import { Component } from 'react'
import { setGlobalData, setStorage, getStorage, getUserInfoId, checkedLoginStatus } from '@utils/tools'
import { hosts } from '@utils/config'
import Taro from '@tarojs/taro';
import vcode, { TrackPage } from "@utils/wx.security.esm";
import './app.scss'
import { View } from '@tarojs/components';


class App extends Component {
  onLaunch = (e) => {
    console.log(e)
  }
  componentDidMount() {
    // Config()
    setStorage('host', getStorage('host') || hosts)
    setGlobalData({
      host: hosts
    })
    Taro.getSystemInfo({
      success: (res) => {
        setGlobalData({
          systemInfo: res,
        });
        // const { platform } = res;
        // setGlobalData({
        //   webpSupport: platform == 'android' || platform == 'devtools',
        // });
      },
    });
    Taro.getNetworkType({
      success: (res) => {
        setGlobalData({
          networkType: res.networkType,
          tabIndex: 0,
          Authorization: checkedLoginStatus()
        });
      },
    });
    setGlobalData({
      headerPosition: Taro.getMenuButtonBoundingClientRect(),
    });
  }

  componentDidShow() {
    TrackPage()
    vcode.init(
      {
        cm: "CN-ZH",
        mid: 'P774',
        mvs: 10000,
        mvn: "1.0.0.0.0",
        alwaysReport: false,   // 可选, 是否离开上报未配置事件
        throttleDelay: 5,
      },
      {
        isDebug: true,
        isUseCache: false,
      }
    );
    let info = Taro.getLaunchOptionsSync()
    if (!!info.referrerInfo?.extraData?.authtoken) {
      Taro.setStorageSync('Authorization', info.referrerInfo?.extraData?.authtoken)
    } else {
      Taro.setStorageSync('Authorization', '')
    }
    vcode.setExtraReport({
      userId: getUserInfoId() || '',
    })
    const updateManager = Taro.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      console.log(res.hasUpdate, '更新') // 请求完新版本信息的回调 true说明有更新
    })
    updateManager.onUpdateReady(function () {
      Taro.showModal({
        title: '更新检测', // 此处可自定义提示标题
        content: '检测到新版本，是否重启小程序？', // 此处可自定义提示消息内容
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    updateManager.onUpdateFailed(function () {
      // 新的版本下载失败
      Taro.showModal({
        title: '更新提示',
        content: '新版本下载失败',
        showCancel: false
      })
    })
  }

  componentDidHide() { }

  componentDidCatchError() { }

  // this.props.children 是将要会渲染的页面
  render() {
    return (
      <>
        <View style='position:fixed;top:0px;left:0;z-index:999;background:#ff0000;'>aiaiiai</View>
        {
          this.props.children
        }
      </>
    )
  }
}

export default App
